#include <jni.h>
#include <string>
#include <zconf.h>
#include "main.h"
#include "JstringUtils.h"
#include "Base64Helper.h"
#include "Log.h"
#include "FileUtils.h"

bool isCheck=false;
extern "C"
JNIEXPORT jstring JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_stringFromJNI(JNIEnv *env, jclass jclass1) {

    const char *source = "qwertyuisfdlsajdxcvnkhsakfh1332487";
    //__android_log_print(ANDROID_LOG_DEBUG, "UTILS:", source);


    string encode = EncryptionAES(source);

    //__android_log_print(ANDROID_LOG_DEBUG, "UTILS:", string2Char(source));

    string decode = DecryptionAES(encode);

    //__android_log_print(ANDROID_LOG_DEBUG, "UTILS:", string2Char(decode));
    return env->NewStringUTF(string2Char(decode));

}

/**
 * AES加密
 */
extern "C"
JNIEXPORT jstring JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_encodeAES(JNIEnv *env, jclass type,
                                                             jstring source_) {
    const char *source = env->GetStringUTFChars(source_, 0);

    string encodeAES = EncryptionAES(source);

    env->ReleaseStringUTFChars(source_, source);


    return env->NewStringUTF(string2Char(encodeAES));
}

/**
 * AES解密
 */
extern "C"
JNIEXPORT jstring JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_decodeAES(JNIEnv *env, jclass type,
                                                             jstring target_) {
    const char *target = env->GetStringUTFChars(target_, 0);

    string decodeAES = DecryptionAES(target);

    env->ReleaseStringUTFChars(target_, target);

    return env->NewStringUTF(string2Char(decodeAES));
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_base64Encode(JNIEnv *env, jclass type,
                                                                jstring source_) {


    LogUtils::Log::debug("TAG  from java:", env->GetStringUTFChars(source_, 0));


    string source = jstring2str(env, source_);

    LogUtils::Log::debug("TAG jstring2char ", source);

    string encode = MUtils::Base64Helper::encode(source);

    LogUtils::Log::debug("TAG  from 加密之后的结果", encode);
    return env->NewStringUTF(string2Char(encode));
}

extern "C"
JNIEXPORT jstring JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_base64Decode(JNIEnv *env, jclass type,
                                                                jstring source_) {
    const char *source = env->GetStringUTFChars(source_, 0);

    LogUtils::Log::debug("", source);
    string decode = MUtils::Base64Helper::decode(source);

    env->ReleaseStringUTFChars(source_, source);

    return env->NewStringUTF(string2Char(decode));
}

extern "C"
JNIEXPORT void JNICALL
Java_com_senjoeson_androidtv_securitydemo_AesUtils_readFile(JNIEnv *env, jclass type,
                                                            jstring filePath_) {
    const char *filePath = env->GetStringUTFChars(filePath_, 0);
    string fileContent = FileUtils::FileUtils::readFile(filePath);
    const char *fileTemp = fileContent.data();

    env->ReleaseStringUTFChars(filePath_, filePath);

    //先转成byte[] char* 转成byte[]
    Byte *byte = (Byte *) fileTemp;


    jbyte *by = (jbyte *) byte;
    size_t len = strlen(fileTemp);
    jbyteArray jarray = env->NewByteArray(len);
    env->SetByteArrayRegion(jarray, 0, len, by);


}